package com.shg.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.shg.bean.Book;
import com.shg.bean.PageView;
import com.shg.bean.SoldBook;
import com.shg.bean.Users;
import com.shg.service.BookService;
import com.shg.service.BookTypeService;
import com.shg.service.SoldBookService;
import com.shg.web.tools.BaseConstrant;
import com.shg.web.tools.FreeMarkerTool;
import com.shg.web.tools.WebTool;

@Controller
public class ViewBookAction {

	//书籍对象
	private Book book;
	//书籍ID
	private String bookid;
	//店铺的ID
	private String shopid;
	//书籍类别ID
	private String typeId;
	//书籍列表
	private List<Book> bookList;
	//书籍列表
	private List<Book> sellerbook;
	//每页显示的记录数
	private int maxrecord=10;
	//当前页码
	private int page=1;
	//分页工具类
	private PageView<SoldBook> pageView;
	//分页工具类
	private PageView<Book> view;
	//销售出去的书籍
	private List<SoldBook> soldBooks;
	//精品推荐
	private List<Book> recommendBook;
	//评价信息列表
	private List<SoldBook> appraiseList;
	//所有店铺的畅销书
	private List<Book> bestBook;
	//新书推荐
	private List<Book> newBook;
	//最收欢迎的书籍
	private List<Book> bookArray;
	//
	private Map<String, Object> bookMap;
	//
	private String ftlName ;
	//
	private String htmlName;
	//id数组列表
	private String [] ids;
	//类型的名称
	private String typeName;
	//
	private String message;
	//用户
	private Users user;
	//注入服务对象
	@Resource  BookService bookService;
	
	@Resource SoldBookService soldBookService;
	
	@Resource BookTypeService bookTypeService;
	
	/**
	 * 主页
	 * @return
	 */
	public String index(){
		return "index";
	}
	/**
	 * 更具ID查询数据信息
	 * @return
	 */
	public String browse(){
		try {
			this.book = (Book) bookService.getBook(book.getId());
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		if(this.book!=null){
			WebTool.getHistory(this.book.getId());
		}
		user=WebTool.getLogin();
		return "browse";
	}
	/**
	 * 浏览历史查询
	 * @return
	 */
	public String history(){
		try {
			this.bookList = bookService.getHistory(Book.class, WebTool.historyId());
		} catch (Exception e) {
			e.printStackTrace();
			this.message="历史查询出现异常";
			return "error";
		}
		return "history";
	}
	/**
	 * 畅销书统计
	 * @return
	 */
	public String bestseller (){
		if(shopid!=null)
			try {
				sellerbook = soldBookService.getBestSeller(shopid, 10);
			} catch (Exception e) {
				e.printStackTrace();
				this.message="操作发生异常";
				return "error";
			}
		return "bestseller";
	}
	/**
	 * 所有店铺数据的前10本畅销书
	 */
	public String bestBook(){
		try {
			bestBook =soldBookService.getBestBook(10);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		return "bestBook";
	}
	/**
	 * 生成静态的HTML
	 */
	public String bestBookMap(){
		try {
			bestBook =soldBookService.getBestBook(10);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		bookMap=new HashMap<String, Object>();
		bookMap.put("bookMap", bestBook);
		FreeMarkerTool.analysisTemplate(BaseConstrant.FTLPATH, ftlName, BaseConstrant.HTMLPATH,htmlName, bookMap);
		return "bestBookMap";
	}
	/**
	 * 静态HTML生成
	 */
	public 	String recommBookMap(){
		bookMap=new HashMap<String, Object>();
		try {
			newBook = bookService.getByIds(ids);
			bookMap.put("bookMap", newBook);
			FreeMarkerTool.analysisTemplate(BaseConstrant.FTLPATH, ftlName, BaseConstrant.HTMLPATH,htmlName, bookMap);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="生成静态HTML抛出异常";
			return "error";
		}
		return "recommBookMap";
	}
	/**
	 * 查询用户的评价信息
	 * @return
	 */
	public String appraise(){
		pageView = new PageView<SoldBook>(maxrecord, page);
		try {
			if(bookid!=null)
				pageView.setQueryResult(soldBookService.getAppraise(pageView, bookid));
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		appraiseList  = pageView.getRecords();
		return "appraise";
	}
	/**
	 * 销售出去的所有书籍
	 */
	public String soldBook(){
		pageView = new PageView<SoldBook>(maxrecord, page);
		try {
			if(bookid!=null)
				pageView.setQueryResult(soldBookService.getSoldRecord(pageView, bookid));
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		soldBooks  = pageView.getRecords();
		return "soldBook";
	}
	/**
	 * 精品推荐
	 * 这里的精品推荐的书籍只是在前台显示，‘
	 * 没有必要进行分页，只需显示前10条即可
	 */
	public String recommend(){
		try {
			recommendBook = bookService.getRecommend(shopid,0, 10).getResultList();
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		return "recommend";
	}
	/**
	 * 首页经典推荐
	 */
	public String recomm(){
		try {
			view=new PageView<Book>(maxrecord, page);
			view.setQueryResult(bookService.getRecommend(view));
		} catch (Exception e) {
			e.printStackTrace();
			this.message="查询经典推荐产生异常";
			return "error";
		}
		return "recomm";
	}
	/**
	 * 首页新书推荐
	 */
	public String newBook(){
		view=new PageView<Book>(maxrecord, page);
		try {
			view.setQueryResult(bookService.getNewBook(null, view.getFirstResult(), view.getMaxrecord()));
		} catch (Exception e) {
			e.printStackTrace();
			this.message="查询最新数据产生异常";
			return "error";
		}
		return "newBook";
	}
	/**
	 * 生成静态的HTML——最新数据
	 */
	public String newBookMap(){
		try {
			newBook = bookService.getByIds(ids);
			bookMap=new HashMap<String, Object>();
			bookMap.put("bookMap", newBook);
			FreeMarkerTool.analysisTemplate(BaseConstrant.FTLPATH, ftlName, BaseConstrant.HTMLPATH,htmlName, bookMap);
		} catch (Exception e1) {
			e1.printStackTrace();
			this.message="查询生成最新数据抛出异常";
			return "error";
		}
		return "newBookMap";
	}
	/**
	 * 最受欢迎的书籍
	 */
	public String love(){
		view=new PageView<Book>(maxrecord, page);
		try {
			bookArray = bookService.getLove(shopid, view).getResultList();
		} catch (Exception e) {
			e.printStackTrace();
			this.message="查询最受欢迎的书籍发生异常";
			return "error";
		}
		return "love";
	}
	/**
	 * 猜 你喜欢的
	 */
	public String youLove(){
		try {
			view =new PageView<Book>(maxrecord, page);
			view.setQueryResult(bookService.getYouLove(typeId, view));
		} catch (Exception e) {
			e.printStackTrace();
			this.message="查询你喜欢的发生异常";
			return "error";
		}
		return "youLove";
	}
	/**
	 * 查询指定类型底下的所有书籍
	 */
	public String typeForBook(){
		view=new PageView<Book>(maxrecord, page);
		try {
			view.setQueryResult(bookService.getTypeList(view, typeId));
		} catch (Exception e) {
			e.printStackTrace();
			this.message="查询书籍分类产生异常";
			return "error";
		}
		return "typeForBook";
	}
	/**
	 * 
	 * 书籍分类查询
	 * @return
	 */
	public String forType(){
		return "fortype";
	}
	/**
	 * 店铺推荐的书籍
	 * @return
	 * 
	 */
	public String shopRecomm(){
		view=new PageView<Book>(maxrecord, page);
		try {
			view.setQueryResult(bookService.getShopRecommend(view));
		} catch (Exception e) {
			e.printStackTrace();
			this.message="查询店铺推荐产生异常";
			return "error";
		}
		return "shopRecomm";
	}
	/**
	 * 店铺、网友推荐
	 * @return
	 * 
	 */
	public String shopRecommMap(){
		try {
			List<Book> books = bookService.getByIds(ids);
			bookMap=new HashMap<String, Object>();
			bookMap.put("bookMap", books);
			FreeMarkerTool.analysisTemplate(BaseConstrant.FTLPATH, ftlName, BaseConstrant.HTMLPATH,htmlName, bookMap);
		} catch (Exception e1) {
			e1.printStackTrace();
			this.message="查询生成网友推荐抛出异常";
			return "error";
		}
		return "shopRecommMap";
	}
	/**公司后台管理
	 * 查询指定类别底下的所有的书籍
	 * 包括子类的书籍
	 * **/
	public String typeBook(){
		view=new PageView<Book>(maxrecord, page);
		try {
			view.setQueryResult(bookService.getTypeList(view, typeId));
		} catch (Exception e) {
			e.printStackTrace();
			this.message="查询出现异常";
			return "error";
		}
		return "typeBook";
	}
	/**
	 * 店铺左边导航的连接查询
	 * 查询类别下的所有书籍
	 */
	public String shopTypeBook(){
		view=new PageView<Book>(maxrecord, page);
		try {
			view.setQueryResult(bookService.getShopTypeList(view,shopid, typeId));
		} catch (Exception e) {
			e.printStackTrace();
			this.message="查询出现异常";
			return "error";
		}
		return "shopTypeBook";
	}
	
	/**
	 * 生成静态的HTML模板
	 * @return
	 */
	public String typeBookMap(){
		try {
			List<Book> modulBook = bookService.getByIds(ids);
			bookMap=new HashMap<String, Object>();
			bookMap.put("bookMap", modulBook);
			bookMap.put("typeName", typeName);
			FreeMarkerTool.analysisTemplate(BaseConstrant.FTLPATH, ftlName, BaseConstrant.HTMLPATH,htmlName, bookMap);
		} catch (Exception e1) {
			e1.printStackTrace();
			this.message="查询生成最新数据抛出异常";
			return "error";
		}
		return "typeBookMap";
	}
	/**
	 * 分页查询最受欢迎的电子书
	 * @return
	 */
	public String bestLove(){
		view=new PageView<Book>(maxrecord, page);
		try {
			view.setQueryResult(bookService.getLove(null, view));
		} catch (Exception e) {
			e.printStackTrace();
			this.message="查询最受欢迎的电子书发生异常";
			return "error";
		}
		return "bestLove";
	}
	/**
	 * 最收欢迎的书籍生成HTML静态
	 * @return
	 */
	public String bestLoveMap(){
		try {
			List<Book> loveBook = bookService.getByIds(ids);
			bookMap=new HashMap<String, Object>();
			bookMap.put("bookMap", loveBook);
			FreeMarkerTool.analysisTemplate(BaseConstrant.FTLPATH, ftlName, BaseConstrant.HTMLPATH,htmlName, bookMap);
		} catch (Exception e1) {
			e1.printStackTrace();
			this.message="查询生成最新数据抛出异常";
			return "error";
		}
		return "bestLoveMap";
	}
	/**
	 * 顶一下
	 * @return
	 */
	public String tip(){
		try {
			bookService.updateLove(bookid);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="顶书发现了异常";
			return "error";
		}
		return "success";
	}
	/**
	 * 禁书界面迁移
	 * @return
	 */
	public String auditingUI(){
		return "auditing";
	}
	/**
	 * 书籍审核
	 */
	public String auditingBook(){
		view=new PageView<Book>(maxrecord, page);
		try {
			view.setQueryResult(bookService.getAuditingBook(view.getFirstResult(), view.getMaxrecord()));
		} catch (Exception e) {
			e.printStackTrace();
			this.message="查询最新数据产生异常";
			return "error";
		}
		return "auditingBook";
	}
	/**
	 * 将书籍设置为禁书
	 * @return
	 */
	public String auditing(){
		view=new PageView<Book>(maxrecord, page);
		try {
			bookService.updateAuditing(book);
			view.setQueryResult(bookService.getAuditingBook(view.getFirstResult(), view.getMaxrecord()));
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		return "auditingBook";
	}
	public List<Book> getBookArray() {
		return bookArray;
	}
	public void setBookArray(List<Book> bookArray) {
		this.bookArray = bookArray;
	}
	public Book getBook() {
		return book;
	}
	public void setBook(Book book) {
		this.book = book;
	}
	public List<Book> getBookList() {
		return bookList;
	}
	public void setBookList(List<Book> bookList) {
		this.bookList = bookList;
	}
	public int getMaxrecord() {
		return maxrecord;
	}
	public void setMaxrecord(int maxrecord) {
		this.maxrecord = maxrecord;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public PageView<SoldBook> getPageView() {
		return pageView;
	}
	public void setPageView(PageView<SoldBook> pageView) {
		this.pageView = pageView;
	}
	public List<SoldBook> getSoldBooks() {
		return soldBooks;
	}
	public void setSoldBooks(List<SoldBook> soldBooks) {
		this.soldBooks = soldBooks;
	}
	public String getBookid() {
		return bookid;
	}
	public void setBookid(String bookid) {
		this.bookid = bookid;
	}
	public String getShopid() {
		return shopid;
	}
	public void setShopid(String shopid) {
		this.shopid = shopid;
	}
	public List<Book> getSellerbook() {
		return sellerbook;
	}
	public void setSellerbook(List<Book> sellerbook) {
		this.sellerbook = sellerbook;
	}
	public List<Book> getRecommendBook() {
		return recommendBook;
	}
	public void setRecommendBook(List<Book> recommendBook) {
		this.recommendBook = recommendBook;
	}
	public List<SoldBook> getAppraiseList() {
		return appraiseList;
	}
	public void setAppraiseList(List<SoldBook> appraiseList) {
		this.appraiseList = appraiseList;
	}
	public List<Book> getBestBook() {
		return bestBook;
	}
	public void setBestBook(List<Book> bestBook) {
		this.bestBook = bestBook;
	}
	public List<Book> getNewBook() {
		return newBook;
	}
	public void setNewBook(List<Book> newBook) {
		this.newBook = newBook;
	}
	public String getFtlName() {
		return ftlName;
	}
	public void setFtlName(String ftlName) {
		this.ftlName = ftlName;
	}
	public String getHtmlName() {
		return htmlName;
	}
	public void setHtmlName(String htmlName) {
		this.htmlName = htmlName;
	}
	public PageView<Book> getView() {
		return view;
	}
	public void setView(PageView<Book> view) {
		this.view = view;
	}
	public String[] getIds() {
		return ids;
	}
	public void setIds(String[] ids) {
		this.ids = ids;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public String getTypeId() {
		return typeId;
	}
	public void setTypeId(String typeId) {
		this.typeId = typeId;
	}
	public String getTypeName() {
		return typeName;
	}
	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}
	public Users getUser() {
		return user;
	}
	public void setUser(Users user) {
		this.user = user;
	}
}
